﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Zeus.Extension.Web
{
    public class WebClientUtil
    {
        public static string GetResponse(string url)
        {
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            string reply = client.DownloadString(url);
            //client.Credentials = CredentialCache.DefaultCredentials;

            client.Dispose();

            return reply;
        }

        /// <summary>
        /// 发送post请求
        /// </summary>
        /// <param name="url">请求的url</param>
        /// <param name="postString">发送到数据 例如:"name=xhan&password=1231"</param>
        /// <param name="encoding">发送和接受数据使用的编码</param>
        /// <returns>服务器响应字符串</returns>
        public static string SendPostRequest(string url, string postString, Encoding encoding)
        {
            string response = "";

            try
            {
                byte[] postData = encoding.GetBytes(postString);

                WebClient client = new WebClient();
                client.Headers.Add("Content-Type", "text/xml");
                client.Headers.Add("ContentLength", postData.Length.ToString());


                byte[] responseData = client.UploadData(url, "POST", postData);

                response = encoding.GetString(responseData);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return response;
        }

        public static string SendSecurityPostRequest(string url, string postString, Encoding encoding, string Username, string Password)
        {
            string response = "";

            try
            {
                byte[] postData = encoding.GetBytes(postString);

                WebClient client = new WebClient();
                client.Headers.Add("Content-Type", "text/xml");
                client.Headers.Add("ContentLength", postData.Length.ToString());
                client.Credentials = new NetworkCredential(Username, Password);
                
                byte[] responseData = client.UploadData(url, "POST", postData);

                response = encoding.GetString(responseData);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return response;
        }

        public static string RetrieveResponse(string uri)
        {
            WebClient client = new WebClient();
            //client.Encoding = Encoding.GetEncoding("GB2312");

            // Add a user agent header in case the 
            // requested URI contains a query.

            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

            Stream data = client.OpenRead(uri);
            StreamReader reader = new StreamReader(data);
            string s = reader.ReadToEnd();
            
            data.Close();
            reader.Close();

            return s;
        }
    }
}
